package com.pangda.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.constant.NoticeConstant;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.Notices;
import com.pangda.pojo.SysUser;
import com.pangda.pojo.vo.NoticesVo;
import com.pangda.service.INoticesService;
import com.pangda.service.ISysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author Panda.No72
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/system/notice")
@Api(tags = "通知管理")
public class NoticesController {
    @Autowired
    private INoticesService noticesService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ISysUserService sysUserService;

    public Notices add(Notices notices) {
        if (NoticeConstant.NOTICETYPE_NOTICE.equals(notices.getNoticeType())) {
            //合同审核通知
            if (NoticeConstant.NOTICE_NEW.equals(notices.getStatus())) {
                notices.setNoticeTitle("通知：有新合同等待审核");
                notices.setNoticeContent("您好," + notices.getCreateBy() + "提交了合同" + notices.getNoticeContent() + "审核申请需要处理！");
               // notices.setNoticeUserId(noticeUserId);
            }

        }
        if (NoticeConstant.NOTICETYPE_ANNOUNCEMENT.equals(notices.getNoticeType())) {
            if (NoticeConstant.NOTICE_NEW.equals(notices.getStatus())) {
                notices.setNoticeTitle("公告：有新活动啦~");
                notices.setNoticeContent("您好," + notices.getCreateBy() + "提交了活动" + notices.getNoticeContent() + "审核申请需要处理！");
               // notices.setNoticeUserId(noticeUserId);
            }

        }


        return notices;
    }

    @GetMapping("/pagelist/{status}")
    public PageResult pagelist(@PathVariable("status") String status) {
        LambdaQueryWrapper<Notices> wrapper = new LambdaQueryWrapper<Notices>().eq(Notices::getStatus, status);
        return PageResult.success(noticesService.page(new Page<>(),wrapper), NoticesVo.class);
    }

    @GetMapping("/{noticeId}")
    public ResponseResult getInfo(@PathVariable("noticeId") Integer noticeId) {
        return ResponseResult.success(noticesService.getById(noticeId));
    }

    @GetMapping("/list/{status}")
    public PageResult list(@PathVariable("status") String status) {
        LambdaQueryWrapper<Notices> wrapper = new LambdaQueryWrapper<Notices>().eq(Notices::getStatus, status);
        return PageResult.success(noticesService.list(wrapper), NoticesVo.class);
    }

    @PutMapping("/{noticeId}")
    public ResponseResult update( @PathVariable("noticeId") Integer noticeId) {
        Notices notice = noticesService.getById(noticeId);
        notice.setNoticeId(noticeId);
        notice.setStatus(NoticeConstant.READ);
        notice.setUpdateBy(UserContext.getUser());
        notice.setUpdateTime(LocalDateTime.now());
        return noticesService.updateById(notice) ? ResponseResult.success() : ResponseResult.error();
    }
}
